<template>
  <div style="display: flex">
    <div class="top flex-row">
      <img
        class="image_top"
        referrerpolicy="no-referrer"
        src="./assets/img/top.png"
      />
    </div>
    <div class="content">
      <!--   查询条件   -->
      <div class="logindiv">
        <div class="loginFormDiv">
          <div class="loginTitle">Register</div>
          <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form">
            <div class="loginTitle1">Email</div>
            <el-form-item prop="username">
              <el-input
                class="input"
                v-model="registerForm.username"
                type="text"
                auto-complete="off"
                placeholder="Please enter your email address"
              >
              </el-input>
            </el-form-item>
            <div class="loginTitle1">Password</div>
            <el-form-item prop="password">
              <el-input
                class="input"
                v-model="registerForm.password"
                type="password"
                auto-complete="off"
                placeholder="Please enter your password"
                @keyup.enter.native="handleLogin"
              >
              </el-input>
            </el-form-item>
            <div class="loginTitle1">ConfirmPassword</div>
            <el-form-item prop="confirmPassword">
              <el-input
                class="input"
                v-model="registerForm.confirmPassword"
                type="password"
                auto-complete="off"
                placeholder="Please enter your confirmPassword"
                @keyup.enter.native="handleRegister"
              >
              </el-input>
            </el-form-item>
            <div class="loginTitle1">Captcha</div>
            <el-form-item prop="code" v-if="captchaOnOff">
              <el-input
                class="input"
                v-model="registerForm.code"
                auto-complete="off"
                placeholder="Please enter your captcha"
                style="width: 63%"
                @keyup.enter.native="handleRegister"
              >
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </el-form-item>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                style="width:100%; background: linear-gradient(#FF3A90 0%, #C6044B 100%); border-radius: 4px;"
                @click.native.prevent="handleRegister"
              >
                <span v-if="!loading">Register</span>
                <span v-else>Registering...</span>
              </el-button>
            </el-form-item>
            <el-form-item style="width: 100%">
              <el-button
                size="medium"
                type="primary"
                style="width:100%; color: #E81566; background: white; border-radius: 4px;"
                @click.native.prevent="handleReg"
              >
                Already have an account
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <div class="footer">
      <div class="footer1">Terms｜About&nbsp;us｜Contact&nbsp;us｜Privacy</div>
      <div class="footer2">Email:topgirlspage88@gmail.com</div>
      <hr>
      <div class="footer3">Copyright&nbsp;©&nbsp;2025&nbsp;topgirlspage.com&nbsp;All&nbsp;Rights&nbsp;Reserved</div>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("The two entered passwords do not match"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "Please enter your email address" },
          { min: 2, max: 60, message: 'Email address must be between 2 and 60 in length', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "Please enter your password" },
          { min: 5, max: 20, message: 'Passwords must be between 5 and 20 in length', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "Please enter your confirmPassword" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "Please enter your captcha" }]
      },
      loading: false,
      captchaOnOff: true
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>Congratulations, your account " + username + " Registration is successful！</font>", 'System prompts', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/cmsLogin");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          })
        }
      });
    },
    handleReg() {
      this.$router.push({ path: "/cmsLogin" });
    }
  },
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 515px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 48px;
    input {
      height: 48px;
      background: #f6f6f6;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.input {
  background-color: #DDDDDD;
}
.loginTitle {
  text-align: center;
  font-size: 28px;
  font-weight: bold;
  padding: 40px 0;
}
.loginFormDiv {
  background-color: #FFFFFF;
  width: 520px;
  height: 658px;
  margin: 30px auto;
}

.logindiv {
  width: 800px;
  height: 740px;
  background: url("assets/img/bg.png");
  margin: 0 auto;
}

.top {
  width: 100%;
  height: 68px;
}

.image_top {
  width: 252px;
  height: 52px;
  margin: 8px 0 0 240px;
}

.flex-row {
  display: flex;
  flex-direction: row;
}

.content {
  width: 100vw;
  background-color: #FFF6F9;
  position: absolute;
  top: 68px;
  bottom: 124px;
  /*left: 240px;*/
}

.footer {
  width: 100%;
  height: 124px;
  background: linear-gradient(#FF3A90 0%, #C6044B 100%);
  position: absolute;
  bottom: 0px;
}

.footer1 {
  text-align: center;
  color: white;
  font-size: 14px;
  margin: 28px 0 0 0;
}
.footer2 {
  text-align: center;
  color: white;
  font-size: 12px;
  margin: 16px 0 0 0;
}
.footer3 {
  text-align: center;
  color: white;
  font-size: 12px;
  margin: 12px 0 13px 0;
}
</style>
